window.onload = function () {
    //从数据库中查询
    var userPhone = localStorage.getItem("currentLoginUserPhone.aomall.com");
    if(userPhone==null||userPhone=="")
    {
        return;
    }
    let Phone = {};
    let result;
    Phone.userPhone = userPhone;
    $.ajax({
        url: "/users/findAddress",
        type: "POST",
        data: JSON.stringify(Phone),
        dataType: "text",
        success: function (resp) {
            result = JSON.parse(resp);//字符串转成JSON对象

            //console.log(result);
            //console.log(result.data);
            //console.log(result.data.length);
            if (result.code === 200) {
                //加载数据
                //将数据展示到页面
                var finish_area = document.querySelector("body > div.MeMainW > div > div > div.MeRightArea > div > div.finished_place > form > div");

                for(var j=0;j<result.data.length;j++)
                {
                    //console.log(result.data[j]);
                    var newul = document.createElement("ul");
                    var li = [];
                    for (var i = 0; i < 6; i++) {
                        var oLi = document.createElement('li');
                        li.push(oLi);
                    }

                    newul.className = "area_list clearfix";

                    li[0].className = "one";
                    li[0].innerText = result.data[j].addressName;
                    newul.appendChild(li[0]);

                    li[1].className = "two";
                    var p = document.createElement("p");
                    p.innerText = "已认证";
                    li[1].appendChild(p);
                    var p1 = document.createElement("p");
                    p1.innerText = result.data[j].addressIdCard.slice(0,3)+"***************";
                    li[1].appendChild(p1);
                    newul.appendChild(li[1]);

                    li[2].className = "three";
                    li[2].innerText = result.data[j].addressProvinces+" "+result.data[j].addressCity+" "+result.data[j].addressCounty+"\n"+result.data[j].addressDetail;
                    newul.appendChild(li[2]);

                    li[3].className = "four";
                    li[3].innerText = result.data[j].addressUserPhone.slice(0,3)+"****"+result.data[j].addressUserPhone.slice(7);
                    newul.appendChild(li[3]);

                    li[4].className = "five";
                    li[4].innerText = "";
                    var bt = document.createElement("button");
                    bt.innerText = "删除";
                    li[4].appendChild(bt);
                    var bt1 = document.createElement("button");
                    bt1.innerText = "修改";
                    li[4].appendChild(bt1);
                    newul.appendChild(li[4]);

                    li[5].className = "six";
                    newul.appendChild(li[5]);

                    finish_area.appendChild(newul);
                }


                //layer.msg(result.msg);
                //注册成功后，两秒钟自动跳转到登录界面。
                //window.setTimeout("javascript:window.location.href = 'login.html'", 5000);
            } else {
                //layer.msg(result.msg)
            }
        }
    });
}

layui.use('layer', function () {
    var layer = layui.layer;
});

function add_address() {
    document.querySelector("body > div.MeMainW > div > div > div.MeRightArea > div > div.finished_place").style.display = "none";
    document.querySelector("body > div.MeMainW > div > div > div.MeRightArea > div > div.new_add").style.display = "block";
    var shen = document.querySelector("#shen");
    for(var i in dist[100000])
    {
        shen.append(new Option(dist[100000][i],i));
        //console.log(dist[100000][i]);
    }
}

function save_address() {
    let address = {};

    address.userPhone = localStorage.getItem("currentLoginUserPhone.aomall.com");

    var shen = document.querySelector("#shen").selectedOptions[0].innerText
    console.log(shen);
    address.addressProvinces = shen;

    var shi = document.querySelector("#shi").selectedOptions[0].innerText
    console.log(shi);
    address.addressCity = shi;

    var xian = document.querySelector("#xian").selectedOptions[0].innerText
    console.log(xian);
    address.addressCounty = xian;

    var detail = document.querySelector("body > div.MeMainW > div > div > div.MeRightArea > div > div.new_add > form > div > div:nth-child(2) > div.right > input[type=text]").value;
    console.log(detail);
    address.addressDetail = detail;
    if(detail.length=="")
    {
        layer.msg("快递员找不到你家！");
        return;
    }

    var name = document.querySelector("body > div.MeMainW > div > div > div.MeRightArea > div > div.new_add > form > div > div:nth-child(3) > div.right > input[type=text]").value;
    console.log(name);
    address.addressName = name;

    var idcard = document.querySelector("body > div.MeMainW > div > div > div.MeRightArea > div > div.new_add > form > div > div:nth-child(4) > div.right > input[type=text]").value;
    console.log(idcard);
    address.addressIdCard = idcard;

    var phone = document.querySelector("body > div.MeMainW > div > div > div.MeRightArea > div > div.new_add > form > div > div:nth-child(5) > div.right > input[type=text]").value;
    console.log(phone);
    address.addressUserPhone = phone;

    var email = document.querySelector("body > div.MeMainW > div > div > div.MeRightArea > div > div.new_add > form > div > div:nth-child(7) > div.right > input[type=text]").value;
    console.log(email);
    address.addressEmail = email;

    var checked = document.querySelector("body > div.MeMainW > div > div > div.MeRightArea > div > div.new_add > form > div > div:nth-child(8) > div").children[0].className=="checkbox checked"?1:0;
    console.log(checked);

    $.ajax({
        url: "/users/addAddress",
        type: "POST",
        data: JSON.stringify(address),
        dataType: "text",
        success: function (resp) {
            let result = JSON.parse(resp);//字符串转成JSON对象

            console.log(resp);
            if (result.code === 200) {
                layer.msg(result.msg);
                window.setTimeout("javascript:window.location.href = 'Member-address.html'", 3000);
            } else {
                layer.msg(result.msg)
            }
        }
    });
}

$("#shen").change(function(){
    console.log(this.value);
    //清楚其他选项
    $("#shi option[value!='']").remove();
    //回复默认值
    $('#shi').each(function (i, j) {
        $(j).find("option:selected").attr("selected", false);
        $(j).find("option").first().attr("selected", true);
    })
    //清楚其他选项
    $("#xian option[value!='']").remove();
    //回复默认值
    $('#xian').each(function (i, j) {
        $(j).find("option:selected").attr("selected", false);
        $(j).find("option").first().attr("selected", true);
    })
    if(this.value!=='')
    {
        var shi = document.querySelector("#shi");
        var id = this.value;
        for(var i in dist[id])
        {
            shi.append(new Option(dist[id][i],i));
            //console.log(dist[id][i]);
        }
    }
})

$("#shi").change(function (){
    console.log(this.value);
    //清楚其他选项
    $("#xian option[value!='']").remove();
    //回复默认值
    $('#xian').each(function (i, j) {
        $(j).find("option:selected").attr("selected", false);
        $(j).find("option").first().attr("selected", true);
    })
    if(this.value!=='')
    {
        var xian = document.querySelector("#xian");
        var id = this.value;
        for(var i in dist[id])
        {
            xian.append(new Option(dist[id][i],i));
            //console.log(dist[id][i]);
        }
    }
})